package web.servlets;

import domain.Item;
import domain.Product;
import persistence.Impl.TIMEtil;
import service.CatelogService;
import service.DialogService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * @projectName: petStore
 * @package: web.servlets
 * @className: ViewItemServlet
 * @author: suqianbei
 * @description: TODO
 * @date: 2021/10/31 15:23
 * @version: 1.0
 */
public class ViewProductServlet extends HttpServlet {
    private static final String VIEW_PRODUCT="/WEB-INF/jsp/catalog/Product.jsp";

    private String productId;
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        productId=req.getParameter("productId");
        CatelogService catelogService =new CatelogService();
        Product product=catelogService.getProduct(productId);
        List<Item> itemList=catelogService.getItemListByProduct(productId);

        //diary 后期账号做好之后 选择判断存储账号还是IP
//        DialogService dialogService=new DialogService();
//        dialogService.insertNewDiary(req.getRemoteAddr(), TIMEtil.getTime(),product.getCategoryId(),product.getProductId(),null,0);

        HttpSession httpSession=req.getSession();
        httpSession.setAttribute("product",product);
        httpSession.setAttribute("itemList",itemList);

        req.getRequestDispatcher(VIEW_PRODUCT).forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
